package com.example.farmer.Fragment.Circle;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Adapter.PostCircleAdapter;
import com.example.farmer.Database.CircleChooseDatabase;
import com.example.farmer.Entity.CirclePost;
import com.example.farmer.Entity.ClassPost;
import com.example.farmer.R;

import java.util.ArrayList;
import java.util.List;

public class FollowFragment extends Fragment {
    private List<String> followList;
//    private String userNumber;
    private RecyclerView recyclerView; // 用于展示帖子列表的RecyclerView
    private  TextView notFollow;
    private List<CirclePost> circlePosts;
    private PostCircleAdapter chooseadapter;
    private CircleChooseDatabase circleChooseDatabase;
    private AppCompatImageView followImageView;
    private final MutableLiveData<List<CirclePost>> followLiveData= new MutableLiveData<>();


    public static FollowFragment newInstance(List<String> followList) {
        FollowFragment fragment = new FollowFragment();
        Bundle args = new Bundle();
        args.putStringArrayList("followList", (ArrayList<String>) followList);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            followList = getArguments().getStringArrayList("followList");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.follow_fragment, container, false);

        // Find the TextView by ID and set the text
        notFollow= view.findViewById(R.id.not_follow);


        //获取登录页面传来的number
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("loginNumber", Context.MODE_PRIVATE);
        String userNumber = sharedPreferences.getString("userNumber" , "") ;

        circleChooseDatabase = CircleChooseDatabase.getInstance(getContext());
        circlePosts = circleChooseDatabase.getAllPosts();

        recyclerView = view.findViewById(R.id.follow); // 确保你的布局文件中有这个ID
        chooseadapter = new PostCircleAdapter(circlePosts,getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(chooseadapter);


        followLiveData.observe(getViewLifecycleOwner(), this::updateUiWithPosts);
        refreshNewsData();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment可见时刷新数据
        refreshNewsData();
    }
    private void refreshNewsData() {
        // 从数据库获取最新数据
        circlePosts = circleChooseDatabase.getAllPosts();
        // 更新LiveData的值
        followLiveData.setValue(new ArrayList<>(circlePosts));
    }
    private void updateUiWithPosts(List<CirclePost> circlePosts) {
        if (circlePosts != null && !circlePosts.isEmpty()) {
            chooseadapter.updatePosts(circlePosts);
            notFollow.setVisibility(View.GONE); // 假设没有数据时显示notNews
        } else {
            notFollow.setText("没有最新内容哦");
            notFollow.setVisibility(View.VISIBLE);
        }
    }
}